package com.zzw.spring.aop.service.impl;

import com.zzw.spring.aop.service.SystemBootAddon;
import com.zzw.spring.aop.service.BenaSelfProxyAware;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class BeanSelfProxyAwareMounter implements SystemBootAddon,ApplicationContextAware{

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public void onReady() {
        Map<String, BenaSelfProxyAware> map = applicationContext.getBeansOfType(BenaSelfProxyAware.class);
        if(map != null){
            for (BenaSelfProxyAware benaSelfProxyAware:map.values()){
                benaSelfProxyAware.setSelfProxy(benaSelfProxyAware);
            }
        }
    }

    @Override
    public int getOrder() {
        return Ordered.HIGHEST_PRECEDENCE;
    }
}
